package makeChanges.controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.Optional;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Category;
import model.CategoryBean;
import model.DatabaseException;
import model.User;
import model.Utilities;

/**
 * Servlet implementation class ChooseCategory
 */
@WebServlet("/ChooseCategory")
public class ChooseCategory extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null || !user.getAccountType().equals("S")){
			response.sendRedirect("Login.html");
			return;
		}	
		session.setMaxInactiveInterval(-1);
		String edit = request.getParameter("edit");
		session.setAttribute("edit", edit);
		try {
			Iterator<CategoryBean> categories = Utilities.getCategories();
			request.setAttribute("categories", categories);
			RequestDispatcher dispatcher = request.getRequestDispatcher("MakeChanges/ChooseCategory.jsp");
			dispatcher.forward(request, response);
		} catch (DatabaseException e) {
			e.printStackTrace();
			response.sendRedirect("TechnicalDifficulties.html");
		}
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null || !user.getAccountType().equals("S")){
			response.sendRedirect("Login.html");
			return;
		}
		request.setCharacterEncoding("UTF-8");
		String categoryName = request.getParameter("categoryName");
		System.out.println(categoryName);
		String minPrice = request.getParameter("minPrice");
		String maxPrice = request.getParameter("maxPrice");
		String minCount = request.getParameter("minCount");
		String maxCount= request.getParameter("maxCount");
		if(minPrice.isEmpty()){
			minPrice=null;
		}
		if(maxPrice.isEmpty()){
			maxPrice=null;
		}
		if(minCount.isEmpty()){
			minCount=null;
		}
		if(maxCount.isEmpty()){
			maxCount=null;
		}
		
		String edit = (String) session.getAttribute("edit");
		try {
			Optional<Category> categoryOptional = Category.getCategory(categoryName,null,null,minPrice,maxPrice, minCount, maxCount, Long.toString(user.getId()));
			if(categoryOptional.isPresent()){
//				System.out.println("in optional for change prices");
				Category category = categoryOptional.get();
				System.out.println(category);
				session.setAttribute("category", category);
				if(edit.equals("delete")||edit.equals("counts")||edit.equals("categorize")){
					response.sendRedirect("/Store/MakeChanges/ChooseProduct.jsp");
				}else if(edit.equals("prices")){
					response.sendRedirect("/Store/MakeChanges/ChangePrices.jsp");
				}
			}
			else{

				response.sendRedirect("/Store/ChooseCategory?edit="+edit);
			}
			
		} catch (DatabaseException e) {
			e.printStackTrace();
			response.sendRedirect("/Store/TechnicalDifficulties.html");
		}
	}

}
